package com.shell.customer.invitation.rx;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.base.dao.CusInvtDao;
import com.shell.customer.invitation.InvtEvent;
import com.shell.customer.po.CusInvt;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class InvtCusSubscriber extends OnErrorNotImplementedSubscriber<InvtEvent> {
	@Autowired
	private CusInvtDao cusInvtDao;

	public void onApplicationEvent(InvtEvent event) {
		CusInvt cusInvt = event.getCusInvt();

		CusRec customer = event.getCustomer();
		CusInvt oldCusInvt = event.getOldCusInvt();
		if ((customer != null) && (oldCusInvt != null) && (customer.getCusName().equals(oldCusInvt.getInviteeName()))) {
			throw new UnCheckMsgException("������������");
		}
		if (customer != null) {
			cusInvt.setInviteeId(customer.getCusId());
		}
		if (oldCusInvt != null) {
			cusInvt.setInvtId(oldCusInvt.getInvtId());
			if (!oldCusInvt.getContactorPhone().equals(cusInvt.getContactorPhone())) {
				cusInvt.setInviterTimes(Integer.valueOf(0));
			}
			if (!oldCusInvt.getInviteeName().equals(cusInvt.getInviteeName())) {
				cusInvt.setCreateTime(DateUtil.getSystemDateTime());
			}
			cusInvt.setEditTime(DateUtil.getSystemDateTime());
			this.cusInvtDao.updateByPrimaryKeySelective(cusInvt);
		} else {
			cusInvt.setInvtId(SystemUtil.getUUID());
			cusInvt.setEditorId(CommonDataUtil.getUserId());
			cusInvt.setInviterTimes(Integer.valueOf(0));
			cusInvt.setEditTime(DateUtil.getSystemDateTime());
			cusInvt.setCreateTime(DateUtil.getSystemDateTime());
			this.cusInvtDao.insert(cusInvt);
		}
	}
}
